//获取当前时间
var d = new Date();
var m = d.getMonth() + 1;
var currentTime = d.getFullYear() + "-" + m + "-" + d.getDate() + " "
		+ d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();// 当前日期
var currentDate = d.getFullYear() + "-" + m + "-" + d.getDate();
//当年1月1日
var startTime=new Date(Date.parse((d.getFullYear()+"-01-01").replace(
		/-/g, "/")));;
		
//获取当年第一天
function getFirstDayOfYear (date) {
    date.setDate(1);
    date.setMonth(0);
    return timeFormat(date);
}

function timeFormat(date) {
	if (!date || typeof (date) === "string") {
		this.error("参数异常，请检查...");
	}
	var y = date.getFullYear(); // 年
	var m = date.getMonth() + 1; // 月
	var d = date.getDate(); // 日

	return y + "-" + m + "-" + d;
}

//页面加载完成
$(function() {
	//短路电路
	$('#checkBtn').click(function(){
		getScatterData();
	});
	
	$("#shortCircuit_tab").on("click", function() {
		var width = parseInt($('#shortCircuitPane').css('width'));
		$("#scatter").css("width", width );
		initScatterChart();
	});
});

$.getScript("ecStat.min.js",function(){});

//短路电路
var scatter;	//散列分布图
var scatterData = [];
var scatterOriginData ;
function getScatterData() {
	var option = getScatterOption();
	$.getJSON('/rest/pms/getShortCircuitInfo/' + JSON.stringify(option),function(result) {
		scatterOriginData = result;
		initScatterData(result);
		initScatterChart();
	});
}


function initScatterData(result){
	scatterData = [];
	for(var i = 0; i < result.length;i++){
		var item = result[i];
		if(item.distance0MomentsTime>0){
			var dis0m = item.distance0MomentsTime/1000;
			if(dis0m > 200){
				continue;
			}
			// [距离故障0时刻的时间，短路电流]
			var itemData = [item.distance0MomentsTime/1000,item.faultValue0];
			scatterData.push(itemData);
		}
	}
}

function getScatterOption() {
	var option = {
		breakerType : '',
		breakerManufacturer : '',
		model : '',
		startDate : '',
		endDate : '',
		maxTime : ''
	};
	option.breakerType = $("#type_circuit").val();
	option.breakerManufacturer = $("#manufacture_circuit").val();
	option.model = $("#model_circuit").val();
	option.startDate = $("#startTime_input").val();
	option.endDate = $("#endTime_input").val();
	
	var maxTime = $("#maxTime").val();
	if(maxTime==""){
        option.maxTime = maxTime;
    } else {
    	option.maxTime = Number.parseFloat(maxTime) * 1000;
	}
	
	return option;
}

function initScatterChart() {
	scatter = echarts.init(document.getElementById('scatter'));
	var clusterNumber = 6;
    var result = ecStat.clustering.hierarchicalKMeans(scatterData, clusterNumber, false);
    var centroids = result.centroids;
    var ptsInCluster = result.pointsInCluster;
    var series = [];

    for (var i = 0; i < centroids.length; i++) {
        series.push({
            name: '时间/电流',	//设置名字
            type: 'scatter',
            data: ptsInCluster[i],
            symbolSize:7,				//图元的大小。改变大小
            markPoint: {
                symbolSize: 20,
                label: {
                    normal: {
                        show: true
                    },
                    emphasis: {
                        show: true,
                        position: 'top',
                        textStyle: {
                            color: '#000'
                        }
                    }
                },
                //数据区域缩放、滚动条
                itemStyle: {
                    normal: {
                        opacity: 0.8
                    }
                },
                scatterData: [{
                    coord: centroids[i]
                }]
            }
        });
    }

    scatter.setOption({
        legend: {
        	data: []
        },
        tooltip: {
            trigger: 'item',
            axisPointer: {
                type: 'cross'
            },
	        formatter : function (params) {
	            if (params.value.length > 1) {
	            	var scaData;
	            	for(var k = 0; k < scatterOriginData.length;k++){
	            		var data = scatterOriginData[k];
	            		if(data.faultValue0 == params.value[1] && data.distance0MomentsTime/1000 == params.value[0]){
	            			scaData = data;
	            			break;
	            		}
	            	}
	                return "线路名称: " + scaData.lineName + '<br/>'
	                	+ "故障时间: " + scaData.faultTime + '<br/>'
	                	+ "变电站: " + (scaData.stationName == ''?"无":scaData.stationName) + '<br/>'
	                	+ "断路器名称: " + (scaData.breakerName == ''?"无":scaData.breakerName) + '<br/>'
	                	+ "故障相别: " + scaData.phase + '<br/>'
	                	+ "故障类型: " + scaData.faultType + '<br/>'
	                	+ "故障原因: "+scaData.faultReason + '<br/>'
	            }
	        }
        },
        xAxis: {
        	name: '时间(ms)',
        	type : 'value',
            scale:true,
            axisLabel : {
                formatter: '{value} ms'
            },
            splitLine: {
                lineStyle: {
                    type: 'dashed'
                }
            },
            series: series 	
        },
        yAxis: {
        	name: '电流(kA)',
        	type : 'value',
            scale:true,
            axisLabel : {
                formatter: '{value} kA'
            },
            splitLine: {
                lineStyle: {
                    type: 'dashed'
                }
            }
        },
        dataZoom: [
                   {
                       type: 'slider',
                       show: false,
                       xAxisIndex: [0],
                       start: 1,
                       end: 100
                   },
                   {
                       type: 'slider',
                       show: false,
                       yAxisIndex: [0],
                       left: '93%',
                       start: 1,
                       end: 100
                   },
                   {
                       type: 'inside',
                       xAxisIndex: [0],
                       start: 1,
                       end: 100
                   },
                   {
                       type: 'inside',
                       yAxisIndex: [0],
                       start: 1,
                       end: 100
                   }
               ],
        series: series,

    });
}
